package it.rockliveitalia;

import java.io.InputStream;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.analytics.tracking.android.EasyTracker;


import android.os.AsyncTask;
import android.os.Bundle;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.text.Html;
import android.text.Spanned;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class EventActivity extends ActionBarActivity {

	private String[] event = new String[13];
	private Bitmap image;
	private ImageView iv;
	private ShareActionProvider mShareActionProvider;
	private String share = null;
	private ProgressBar pb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_event);

		Intent intent = getIntent();
		event = intent.getStringArrayExtra("eventDetails");

		pb = (ProgressBar) findViewById(R.id.imgProgress);
		iv = (ImageView) findViewById(R.id.photo);
		
		new TheTask().execute();
		showEvent();
	}
	
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onStop() {
		super.onStop();
	    EasyTracker.getInstance(this).activityStop(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.event, menu);
		MenuItem shareItem = menu.findItem(R.id.social_share);
		mShareActionProvider = (ShareActionProvider) MenuItemCompat
				.getActionProvider(shareItem);
		mShareActionProvider.setShareIntent(share());
		return super.onCreateOptionsMenu(menu);
	}

	private Intent share() {
		Intent i = new Intent(Intent.ACTION_SEND);
		i.setType("text/plain");
		i.putExtra(Intent.EXTRA_TEXT, share);
		return i;
	}

	private void showEvent() {
		TextView venueName = (TextView) findViewById(R.id.venueName);
		TextView venueSite = (TextView) findViewById(R.id.venueSite);
		TextView date = (TextView) findViewById(R.id.date);
		TextView address = (TextView) findViewById(R.id.address);
		TextView band = (TextView) findViewById(R.id.band);
		TextView bandSite = (TextView) findViewById(R.id.bandSite);
		TextView description = (TextView) findViewById(R.id.description);
	
		band.setText(event[7]);
		date.setText(event[3]);
		venueName.setText(event[1]);
		address.setText(event[4] + ", " + event[5] + " (" + event[6] + ")");
		description.setText(event[9]);
		bandSite.setText(event[8]);
		venueSite.setText(event[2]);
		
		share =event[7]+" live @ "+event[1]+" "+event[5]+" (" + event[6] + "), "+event[3]+" via Rock Live Italia";

	}

	class TheTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			try {
				String url = event[10];

				image = downloadBitmap(url);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (image != null) {
				iv.setImageBitmap(image);
				pb.setVisibility(View.GONE);
				
			}
		}
	}

	private Bitmap downloadBitmap(String url) {
		// initilize the default HTTP client object
		final DefaultHttpClient client = new DefaultHttpClient();

		// forming a HttoGet request
		final HttpGet getRequest = new HttpGet(url);
		try {

			HttpResponse response = client.execute(getRequest);

			// check 200 OK for success
			final int statusCode = response.getStatusLine().getStatusCode();

			if (statusCode != HttpStatus.SC_OK) {
				return null;
			}

			final HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream inputStream = null;
				try {
					// getting contents from the stream
					inputStream = entity.getContent();

					// decoding stream data back into image Bitmap that android
					// understands
					image = BitmapFactory.decodeStream(inputStream);

				} finally {
					if (inputStream != null) {
						inputStream.close();
					}
					entity.consumeContent();
				}
			}
		} catch (Exception e) {
			// You Could provide a more explicit error message for IOException
			getRequest.abort();
			e.printStackTrace();
		}

		return image;
	}
	
	public void eventOnMap(View view){
		List<String> ids = new ArrayList<String>();
		ids.add(event[0]);
		Intent intent = new Intent(this, MapFilterActivity.class);
		intent.putStringArrayListExtra("ids",
				(ArrayList<String>) ids);
		intent.putExtra("filter", "Evento");
		startActivity(intent);
	}
	
	@Override
	  public void onStart() {
	    super.onStart();
	    EasyTracker.getInstance(this).activityStart(this); 
	  }
	

}
